/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor.Controlador;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jorge Perez Martin
 */
public class MainController {

    private static int port = 50;

    public static void main(String[] args) {

        System.out.println("Arrancando servidor");
        ServerSocket servSock = null;
        try {
            servSock = new ServerSocket(port);
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("Esperando conexiones...");
        while (true) {
            try {
                Socket incoming = servSock.accept();
                BufferedReader read = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                System.out.println("Fijado el read");
                PrintWriter write = new PrintWriter(incoming.getOutputStream(), true);
                System.out.println("Fijado el write");

                String order = null;

                try {
                    System.out.println("Esperando linea");
                    order = read.readLine();
                    System.out.println("La orden enviada es la: " + order);

                } catch (IOException ex) {
                    System.out.println("ERROR con la IO");
                }
                if (order.isEmpty()) {
                    read.close();
                    incoming.close();
                } else {
                    OcaTask t = null;
                    if (order.equalsIgnoreCase(ConnectionConstraints.LOGIN)) {
                        t = new LoginTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_NEW)) {
                        t = new NewGameTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_START)) {
                        t = new StartGameTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_THROW_DICE)) {
                        t = new ThrowDiceTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_EXIT)) {
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_JOIN)) {
                        t = new JoinGameTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_FINISH)) {
                    } else {
                        t = new TrashTask(incoming, read, write);
                    }
                    t.executeTask();
                }

            } catch (UnknownHostException ex) {
                Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
